package com.example.keli.ui.my.star;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseFragment;
import com.example.keli.base.utils.DensityUtil;
import com.example.keli.ui.my.adapter.StarAdapter;
import com.example.keli.ui.my.model.StarVO;
import com.example.keli.ui.userinfo.UserInfoDialog;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

/**
 * description $desc$
 * created by jerry on 2019/3/18.
 */
public class StarFragment extends BaseFragment {

    private static final String TAG = "CommissionFragment";
    @BindView(R.id.recycler_star)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout mRefreshlayout;
    private StarAdapter starAdapter;
    private List<StarVO> mStars;
    private int mType = 0;//0 被点赞   1我点赞的

    public static StarFragment newInstance(int type) {
        StarFragment aboutFragment = new StarFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        aboutFragment.setArguments(bundle);
        return aboutFragment;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_star;
    }

    @Override
    protected void bindData() {
        Bundle bundle = getArguments();
        if (bundle != null){
            mType = bundle.getInt("type");
        }
        initRecycleView();
        loadDatas();
    }

    private void initRecycleView() {
        mStars = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        starAdapter = new StarAdapter(mStars, getContext(), mType);
        recyclerView.setAdapter(starAdapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .color(ContextCompat.getColor(getActivity(), R.color.divider_line))
                .size(DensityUtil.dip2px(getActivity(), 1))
                .build());

        View emptyView = View.inflate(getContext(), R.layout.empty_view, null);
        TextView text = emptyView.findViewById(R.id.tv_empty);
        text.setText("暂无记录");
        starAdapter.setEmptyView(emptyView);
    }

    @Override
    protected void bindListener() {
        mRefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadDatas();
            }
        });
        starAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                StarVO starVO = (StarVO) adapter.getItem(position);
                if (starVO != null){
                    int uid = (mType==0 ? starVO.getUid() : starVO.getBy_uid());
                    new UserInfoDialog(getActivity(), uid).show();
                }
            }
        });
    }

    private void loadDatas() {
        Map<String, String> map = new HashMap<>();
        String act = mType == 0 ? "my" : "myLike";
        map.put("act", act);
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getMyLike(map), new SubscriberListener<BaseResponse<List<StarVO>>>() {
            @Override
            public void onSuccess(BaseResponse<List<StarVO>> listBaseResponse) {
                mStars.clear();
                mStars.addAll(listBaseResponse.data);
                starAdapter.notifyDataSetChanged();
                mRefreshlayout.setRefreshing(false);
            }

            @Override
            public void onFailer(String msg) {
                mRefreshlayout.setRefreshing(false);
            }

            @Override
            public void onTokenError() {
                mRefreshlayout.setRefreshing(false);
            }
        });
    }
}
